/*
 * firings.cpp
 *
 *  Created on: 11.04.2012
 *      Author: delgado
 */
#include "firings.h"

#include <cstdlib>
using std::rand;
using std::srand;
#include <ctime>
using std::time;

using std::cout;
using std::endl;

namespace neurignacio{

Pattern::Pattern(const number_t n, const real a) : list< list<Neuron> >(), n(n), a(a)
{
	// Calculate the number of active neurons
	number_t nActive = n*a;
	// Create Z0
	NeuronList Z0;
	for (Neuron i=0; i<nActive; ++i)
		Z0.push_back(i);
	this->push_back(Z0);
}

void Pattern::generate(const number_t m)
{
	// Reset the seed
	srand(time(NULL));
	// Populate the list
	for (number_t p=0; p<m; ++p)
	{
		NeuronList Zp;
		for (Neuron i=0; i<n; ++i)
			if ( static_cast<real>(rand()) / RAND_MAX < a)
				Zp.push_back(i); // Add neurons to the pattern list Zp
		this->push_back(Zp);	// Add the pattern 'Zp' to the pattern object
	}
}

vector<data_t> Pattern::AsVector(const number_t pattern)
{
//	cout << "Pattern::AsVector" << endl;
	Pattern::iterator p=this->begin();	// Create iterator pointing to begin
	// Go to 'pattern' position
	for (number_t position=0; position<pattern; ++position)
		++p;
//	cout << "p->size(): " << p->size() << " ";
	vector<data_t> X(n,0);	// Create Output vector filled with 0
	number_t neuronCounter=0;
	for (NeuronList::iterator i=p->begin(); i!=p->end(); ++i, ++neuronCounter)
	{
//		cout << *i << " ";
		X[*i]=1;

	}
//	cout << " neuronCounter=" << neuronCounter << endl;
//	cout << "Result:" << endl;
//	for (vector<data_t>::iterator it=X.begin(); it!=X.end(); ++it)
//		cout << (int)*it << " ";
//	cout << endl;
	return X;
}

ostream& operator<<(ostream& out, const Pattern& pattern)
{
	number_t counter=0;
	for (Pattern::const_iterator p=pattern.begin(); p!=pattern.end(); ++p, ++counter)
	{
		out << "#" << counter << ": [ ";
		for (NeuronList::const_iterator i=p->begin(); i!=p->end(); ++i)
			out << (int)*i << " ";
		out << "]" << endl;
	}
	return out;
}

}	// end namespace neurignacio

